package com.chilly.io;

import java.io.*;

/**
 * 对象 和 字节数组 之间相互转换
 * Created by Chilly Cui on 2020/9/24.
 */
public class UtilObject2Byte {

    public static <T> byte[] Object2Byte(T t) throws IOException {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(out);
        oos.writeObject(t);
        return out.toByteArray();
    }

    public static <T> T Byte2Object(byte[] data) throws IOException, ClassNotFoundException {
        ByteArrayInputStream in = new ByteArrayInputStream(data);
        ObjectInputStream ois = new ObjectInputStream(in);
        T t = (T) ois.readObject();
        return t;
    }
}
